package viewCollection;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TreeMap;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import util.Messages;
import dataCollection.AbstractDetailTreeAlgorithms;
import dataCollection.Dice;
import dataCollection.Trait;
import dataCollection.AbstractDetail;
import dataCollection.Tree;

public class TraitsView {
  
  private JPanel main_panel_;
  private InfoPanel info_panel_;
  private Vector<Trait> traits_;
  Vector<TraitHandler> trait_handlers_;
  DefaultComboBoxModel<Dice> model;
    public class TraitHandler {
    private JComboBox<Dice> combo_box_; 
    private BoxModelDecorator deco_;
    public JButton button_;
    public final String name_;
    public TraitHandler(JComboBox<Dice> box, 
                        JButton button, String name,
                        DefaultComboBoxModel<Dice> original_deco) {
      name_ = name;
      combo_box_ = box;
      button_ = button;
      deco_ = new BoxModelDecorator(original_deco);
    }
    
    public void removeElement(Dice d) {
      button_.setText(Messages.getString("Reset"));
      //combo_box_.setSelectedIndex(-1);
      combo_box_.setEnabled(false);
      for(TraitHandler t : trait_handlers_) {
        if(t.combo_box_.isEnabled() && 
           t.combo_box_.getSelectedIndex() == combo_box_.getSelectedIndex()) {
          t.combo_box_.setSelectedIndex(-1);
        }
      }
      deco_.removeElement(d);
      combo_box_.setEditable(true);
      combo_box_.setSelectedIndex(-1);
      combo_box_.setSelectedItem(d);
    }
    
    public Dice getSelectedItem() {
      return (Dice)combo_box_.getSelectedItem();
    }

    public void restoreElement(Dice dice) {
      button_.setText(Messages.getString("OK"));
      deco_.addElement(dice);
      combo_box_.setEditable(false);
      combo_box_.setSelectedIndex(0);
      combo_box_.setEnabled(true); 
    }
  };
  


  public TraitsView(JPanel main_panel, InfoPanel info_panel, Tree<AbstractDetail> ability_tree) {
    main_panel_ = main_panel;
    info_panel_ = info_panel;
    traits_ = AbstractDetailTreeAlgorithms.getAllTraits(ability_tree);
    model = new DefaultComboBoxModel<Dice>();
    trait_handlers_ = new Vector<TraitHandler>();
    
    for (Trait trait: traits_) {
      BoxModelDecorator deco = new BoxModelDecorator(model);
      JComboBox<Dice> box = new JComboBox<Dice>(deco);
      JButton button = new JButton(Messages.getString("OK"));
      TraitHandler trait_handler = new TraitHandler(box, button, trait.getName(), model);
      trait_handlers_.add(trait_handler);
    }
  }
  
  public void paintTraits(Vector<Dice> dice_vec) {
    for(Dice d : dice_vec) {
      model.addElement(d);
    }
    main_panel_.removeAll();
    main_panel_.setLayout(new GridLayout(0,3,10,10));
    int i = 0;
    JLabel lab0 = new JLabel("");
    JLabel lab1 = new JLabel(Messages.getString("Physical"));
    JLabel lab2 = new JLabel("");
    main_panel_.add(lab0);
    main_panel_.add(lab1);
    main_panel_.add(lab2);
    for (Trait trait: traits_) {
      if(!trait.isPhysical()) {
        ++i;
        continue; 
      }
      JLabel lab = new JLabel(trait.getName());

      main_panel_.add(lab);
      main_panel_.add(trait_handlers_.elementAt(i).combo_box_);
      main_panel_.add(trait_handlers_.elementAt(i).button_);
      info_panel_.setInfoText(lab, trait.getDesc());
      info_panel_.setInfoText(trait_handlers_.elementAt(i).combo_box_, trait.getDesc());
      info_panel_.setInfoText(trait_handlers_.elementAt(i).button_, trait.getDesc());
      ++i;
    }
    JLabel lab3 = new JLabel("");
    JLabel lab4 = new JLabel(Messages.getString("Mental"));
    JLabel lab5 = new JLabel("");
        
    main_panel_.add(lab3);
    main_panel_.add(lab4);
    main_panel_.add(lab5);
    i = 0;
    for (Trait trait: traits_) {
      if(trait.isPhysical()) {
        ++i;
        continue; 
      }
      JLabel lab = new JLabel(trait.getName());

      main_panel_.add(lab);
      main_panel_.add(trait_handlers_.elementAt(i).combo_box_);
      main_panel_.add(trait_handlers_.elementAt(i).button_);

      info_panel_.setInfoText(lab, trait.getDesc());
      info_panel_.setInfoText(trait_handlers_.elementAt(i).combo_box_, trait.getDesc());
      info_panel_.setInfoText(trait_handlers_.elementAt(i).button_, trait.getDesc());
      ++i;
    }
    main_panel_.revalidate();
    main_panel_.doLayout();
  }
  
  public void setButtonAction(ActionListener e) {
    for(TraitHandler trait : trait_handlers_) {
      trait.button_.addActionListener(e);
    }
  }
  
  public Vector<TraitHandler> getTraits() {
    return trait_handlers_;
  }

}
